<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
require_once dirname(__FILE__) . '/../../../incls/php/ecomutils.class.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'update_attr':
      update_attr($myPdo);
      break;
    case 'cancel_order':
      cancel_order($myPdo);
      break;
    case 'restore_order':
      restore_order($myPdo);
      break;
    case 'print_order':
      print_order($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function update_attr($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $attr = MiscUtils::getParam('attr', NULL);
  if ($attr) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1';
    $cond_vals->v = array(':v1' => MiscUtils::getParam('id', 0));
    $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($tmp) > 0) {
      $obj = $tmp[0];
      $obj->$attr = MiscUtils::getParam('value', '');
      if ($attr == 'is_delivered') {
        $obj->date_deliver = MiscUtils::getParam('date', '0000-00-00');
        DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $obj);
      }
      if ($attr == 'is_paid') {
        DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $obj);
        if ($obj->customer_id != 0) {
          EComUtils::update_customer($myPdo, $obj->customer_id);
        }
      }
    }
  }
  echo json_encode(array());
}

function cancel_order($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $id = MiscUtils::getParam('id', 0);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.order_number = :v1';
  $cond_vals->v = array(':v1' => $id);
  $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmp) > 0) {
    $tmp[0]->is_cancel = 1;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $tmp[0]);
    EComUtils::update_stock($myPdo, $tmp[0], true);
  }
  echo json_encode(array());
}

function restore_order($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $id = MiscUtils::getParam('id', 0);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.order_number = :v1';
  $cond_vals->v = array(':v1' => $id);
  $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmp) > 0) {
    $tmp[0]->is_cancel = 0;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $tmp[0]);
    EComUtils::update_stock($myPdo, $tmp[0], false);
  }
  echo json_encode(array());
}

function print_order() {
  require_once dirname(__FILE__) . '/../../../libs/MPDF56/mpdf.php';
  $mpdf = new mPDF('c'); 
  $mpdf->SetDisplayMode('fullpage');
  // LOAD a stylesheet
  $stylesheet = file_get_contents('http://' . URL . 'incls/php/ecomorder.print.php?secret_pass=0&id=' . $my_order->order_number); 
  $mpdf->WriteHTML($stylesheet);
  $mpdf->Output();
}
?>